//
//  ShowTitleImageView.m
//  PetTown
//
//  Created by lanou3g on 15/5/28.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ShowTitleImageView.h"
#import "JSONKit.h"
#import "UIImageView+WebCache.h"
#define LHeight self.frame.size.height
#define LWidth  self.frame.size.width
@implementation ShowTitleImageView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


//初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //新建scrollView
        self.scrollView  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,LWidth, LHeight/2+ 10)];
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        
        _scrollView.backgroundColor = [UIColor clearColor];
        [self addSubview:_scrollView];
        //新建pageControl
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(LWidth-120, LHeight/2-20, 100, 34)];
        //设置pagecontrol属性
        _pageControl.currentPage = 0;
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [self addSubview:_pageControl];
        
        self.leftButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.leftButton.frame = CGRectMake(5, LHeight/2 + 15, (LWidth - 30)/3, (LWidth - 30)/3);
        
        self.centerButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.centerButton.frame = CGRectMake((LWidth - 30)/3 + 15, LHeight/2 + 15, (LWidth - 30)/3, (LWidth - 30)/3);
        self.rightButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _rightButton.frame = CGRectMake((LWidth - 30)/3 * 2 + 25, LHeight/2 + 15, (LWidth - 30)/3, (LWidth - 30)/3);

        [self addSubview:_leftButton];
        [self addSubview:_centerButton];
        [self addSubview:_rightButton];
    }
    return self;
}

//set方法
-(void)setCarouselArray:(NSMutableArray *)carouselArray{
    _carouselArray = carouselArray;
    [self setSHowScrollView];
}
-(void)setHotlistArray:(NSMutableArray *)hotlistArray{
    _hotlistArray = hotlistArray;
    [self setSHowButton];
}



//添加内容scroll
- (void)setSHowScrollView{
    //设置contentsize
    _scrollView.contentSize = CGSizeMake(_carouselArray.count*CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame));
    //设置pagecontrol属性
    _pageControl.numberOfPages = _carouselArray.count;
    //将图片添加到scroller上
    for (int i = 0; i< _carouselArray.count; i++) {
        UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
        NSDictionary * dic = _carouselArray[i];
        NSURL * url = [NSURL URLWithString:[dic objectForKey:@"img"]];
        [imgView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"place.jpg"]];
        [_scrollView addSubview:imgView];
    }
    
}

- (void)setSHowButton{
    //将图片添加到button上
    NSDictionary * leftdic = _hotlistArray[0];
    NSDictionary * centerdic = _hotlistArray[1];
    NSDictionary * rightdic = _hotlistArray[2];
    NSURL * lefturl = [NSURL URLWithString:[leftdic objectForKey:@"coverimg"]];
    NSURL * centerurl = [NSURL URLWithString:[centerdic objectForKey:@"coverimg"]];
    NSURL * righturl = [NSURL URLWithString:[rightdic objectForKey:@"coverimg"]];
    UIImageView * leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (LWidth - 30)/3, (LWidth - 30)/3)];
    UIImageView * centerImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (LWidth - 30)/3, (LWidth - 30)/3)];
    UIImageView * rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (LWidth - 30)/3, (LWidth - 30)/3)];
    [leftImg sd_setImageWithURL:lefturl placeholderImage:[UIImage imageNamed:@"Stars@2x.png"]];
    [centerImg sd_setImageWithURL:centerurl placeholderImage:[UIImage imageNamed:@"Stars@2x.png"]];
    [rightImg sd_setImageWithURL:righturl placeholderImage:[UIImage imageNamed:@"Stars@2x.png"]];
    [_leftButton addSubview:leftImg];
    [_centerButton addSubview:centerImg];
    [_rightButton addSubview:rightImg];

}

@end
